home *** CD-ROM | disk | FTP | other *** search
- Path: news.microsoft.com!news
- From: a-cnadc@microsoft.com (Dann Corbit)
- Newsgroups: comp.lang.c
- Subject: Re: Finding a prime number
- Date: 1 Feb 1996 18:09:09 GMT
- Organization: Microsoft Corporation
- Message-ID: <4eqvk5$7tn@news.microsoft.com>
- References: <4e875s$nqk@reader2.ix.netcom.com>
- NNTP-Posting-Host: 157.57.171.202
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.14
-
- In article <4e875s$nqk@reader2.ix.netcom.com>, advtr@ix.netcom.co says...
- >
- >I need to write a function that will find wether or not a number is
- >prime. I can come close but I get numbers that are not prime with the
- >prime numbers.
- >Here is the function I wrote. Any help would be great. Thanks Ken(A
- >begining C programmer)
- >
- >int primenumber(int operand)
- >{/*Start primenumber*/
- > int two = 0,three = 0,four = 0,five = 0,six = 0,seven = 0;
- > int eight = 0,nine = 0;
- >
- >
- > two =(operand%2);
- > three =(operand%3);
- > four = (operand%4);
- > five = (operand%5);
- > six = (operand%6);
- > seven = (operand%7);
- > eight = (operand%8);
- > nine = (operand%9);
- >
- > if (two ==0||three==0||four==0||five==0||six==0
- > ||seven==0||eight==0||nine==0)
- > return 0;
- > else
- > return 1;
- >}/*End primenumber*/
- You only need to test division by primes (though the other tests
- don't hurt anything, they just make it slower). You also quit
- testing too soon for any number bigger than 49. If you want to
- use modulo testing, you should divide by all primes up to the
- square root of the input number.
- --
- The opinions expressed in this message are my own personal views
- and do not reflect the official views of Microsoft Corporation.
-
-